<template>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>权限管理</el-breadcrumb-item>
        <el-breadcrumb-item>角色列表</el-breadcrumb-item>
      </el-breadcrumb>
      <el-card>
        <el-row>
          <el-col>
            <el-button type="primary">添加角色</el-button>
          </el-col>
        </el-row>
        <el-table :data="roleList" border stripe>
          <el-table-column type="expand">
            <template slot-scope="scope">
              <el-row :class="['bdbottom',i==0?'bdtop':'','vcenter']" v-for="(item,i) in scope.row.children" :key="item.id">
                <el-col :span="5">
                  <el-tag>{{item.authName}}</el-tag>
                  <i class="el-icon-caret-right"></i>
                </el-col>
                <el-col :span="19">
                  <el-row :class="[i2==0?'':'bdtop','vcenter']" v-for="(item2,i2) in item.children" :key="item2.id">
                    <el-col :span="8">
                      <el-tag type="success">{{item2.authName}}</el-tag>
                      <i class="el-icon-caret-right"></i>
                    </el-col>
                    <el-col :span="16">
                      <el-tag style="margin-right: 10px"
                              v-for="(item3,i3) in item2.children"
                              :key="item3.id"
                              closable
                              @close="removeTag(scope.row,item3.id)"
                      >{{item3.authName}}</el-tag>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </template>
          </el-table-column>
          <el-table-column type="index"></el-table-column>
          <el-table-column label="角色名称" prop="roleName"></el-table-column>
          <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
          <el-table-column label="操作" width="300px">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
              <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
              <el-button type="warning" icon="el-icon-setting" size="mini" @click="setUserRight(scope.row)">分配权限</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!--分配权限对话框-->
      <el-dialog
        title="分配权限"
        :visible.sync="setRightDialogVisible"
        width="50%"
        @close="setUserRightClose"
      >
       <!-- 树形控件-->
        <el-tree
          :data="rightList"
          :props="treeProps"
          show-checkbox
          node-key="id"
          default-expand-all
          :default-checked-keys="defKey"
          ref="treeRef"
        >
        </el-tree>
        <span slot="footer" class="dialog-footer">
          <el-button @click="setRightDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="allotRights">确 定</el-button>
        </span>
      </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "Roles",
        data(){
            return{
                roleList:[],
                setRightDialogVisible:false,
                rightList:[],
                //树形控件属性
                treeProps:{
                  children: 'children',
                  label: 'authName'
                },
                //默认选中权限id数组
                defKey:[],
                //当前即将分配权限的角色id
                roleId:'',
            }
        },
        created() {
            this.getRoleList();
        },
        methods:{
            getRoleList(){
                this.$http.get('roles').then(res=>{
                    if(res.data.meta.status==200){
                        this.roleList=res.data.data;
                    }else{
                        this.$message.error('获取数据失败');
                    }
                });
            },
            removeTag(role,rightId){
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //请求删除接口
                    this.$http.delete(`roles/${role.id}/rights/${rightId}`).then(res=>{
                        if(res.data.meta.status==200){
                          this.$message({
                            type: 'success',
                            message: '删除成功!'
                          });
                          role.children=res.data.data;
                        }
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //分配权限
            setUserRight(role){
                this.roleId=role.id;
                //获取角色权限
                this.$http.get('rights/tree').then(res=>{
                    if(res.data.meta.status=!200){
                       this.$message.error("获取权限数据失败");
                    }else{
                       //获取到的权限数据
                       this.rightList=res.data.data;
                      //递归获取所有三级权限的id
                      this.getDefKeys(role,this.defKey);
                      this.setRightDialogVisible=true;
                    }
                });
            },
          //通过递归形式获取所有三级权限的id
          getDefKeys(node,arr){
              if(!node.children){
                 return arr.push(node.id);
              }
              node.children.forEach(item=>{
                this.getDefKeys(item,arr);
              })
          },
          //关闭弹框清空defKey数组
          setUserRightClose(){
              this.defKey=[];
          },
          //确认关闭弹窗发送分配权限请求
          allotRights(){
              const keys=[
                  ...this.$refs.treeRef.getCheckedKeys(),
                  ...this.$refs.treeRef.getHalfCheckedKeys(),
              ];
              const idStr=keys.join(",");
              console.log(this.roleId);
              //发送请求
              this.$http.post(`roles/${this.roleId}/rights`,{rids:idStr}).then(res=>{
                console.log(res);
                if(res.data.meta.status!=200){
                    this.$message.error("权限分配失败");
                }else{
                  this.$message.success("分配权限成功");
                  this.getRoleList();
                  //关闭弹窗
                  this.setRightDialogVisible = false;
                }
            })
          }
        }
    }
</script>

<style lang="less" scoped>
.el-tag{
  margin-top: 7px;
  margin-bottom: 7px;
}
.bdbottom {
  border-bottom: 1px solid #eee;
}
.bdtop{
  border-top:1px solid #eee ;
}
.vcenter{
  display: flex;
  align-items: center;
}
</style>
